from django.shortcuts import render,redirect
from django.http import HttpResponse
from web.models import Category


# Create your views here.
from django.template.loader import render_to_string
def create_category_type():
    return [
        {"value":0,"name":"导航栏目"},
        {"value": 1, "name": "文章列表栏目"},
        {"value": 2,"name": "内容栏目"},
        {"value": 3,"name": "图片栏目"},
        {"value": 4,"name": "超链接栏目"},
    ]

def category_find(request):
    cs = Category.objects.all()
    context = {"cs":cs}
    return render(request,"web/category/find.html",context=context)


def category_add_input(request):
    context = {
        "type":create_category_type(),
        "ps":Category.objects.all()
    }
    return render(request, "web/category/add.html",context = context)

def category_add(request):
    pid = int(request.POST.get("pid"))
    print(pid)
    c = Category()
    if pid!=0:
        # c.parent = Category()
        p = Category.objects.get(id=pid)
        print(p)
        c.parent = p
    c.name = request.POST.get("name")
    c.href = request.POST.get("href")
    c.type = request.POST.get("type")
    c.is_index = request.POST.get("is_index")
    c.is_nav = request.POST.get("is_nav")
    c.orders = request.POST.get("orders")
    c.save()
    return redirect("/web/category/find")


def category_del(request,id):
    c = Category.objects.get(id=id)
    c.delete()
    return redirect("/web/category/find")


def category_update_input(request,id):
    c = Category.objects.get(id=id)
    ps = Category.objects.all()
    context = {
        "c":c,
        "type":create_category_type(),
        "ps":ps
    }
    return render(request,"web/category/update.html",context=context)

def topic_find(request):
    pass